1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution(object): def helpler(self, l, r, item, res): if r < l: # print item return if l == 0 and r == 0: res.append(item) if l > 0: self.helpler(l - 1, r, item + '(', res) if r > 0: self.helpler(l, r - 1, item + ')', res) def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ if n == 0: return [] res = [] self.helpler(n, n, '', res) return res
|